package cn.kgc.controller;

import cn.kgc.pojo.Houserole;
import cn.kgc.service.HouseroleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-04 10:00
 */
@Controller
public class AssignHandler {
    @Resource
    HouseroleService houseroleService;

    @RequestMapping("/assign/to/assign/role/page")
    public String toAssignRolePage(Integer userId, Integer pageNum, Model model) {
        //查询已分配的juese
        List<Houserole> assignedRoleList = houseroleService.getAssignedRoleList(userId);
        //查询未分配角色
        List<Houserole> unAssignedRoleList = houseroleService.getUnAssignedRoleList(userId);
        //存储数据
        model.addAttribute("assignedRoleList", assignedRoleList);
        model.addAttribute("unAssignedRoleList", unAssignedRoleList);
        return "body/user/assign-user";
    }

    @RequestMapping("/assign/role")
    public String assignRole(@RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList,
                             @RequestParam("userId") Integer userId,
                             @RequestParam("pageNum") String pageNum) {

        //更新关系
        houseroleService.updateRelationship(userId, roleIdList);
        return "redirect:/user/query/for/search?pageNum=" + pageNum;
//        /user/query/for/search
    }
}
